<template>
    <div class="app-container">
        <div class="customContainer ">
            <!-- 搜索头 -->
            <el-form :inline="true" :model="formInline" ref="formInline" class="demo-form-inline" size="small">
                <el-form-item>
                    <el-date-picker :editable="false" :clearable="false" v-model="pickerDate" value-format="yyyy.MM.dd"
                        type="daterange" align="right" unlink-panels range-separator="-" start-placeholder="开始日期"
                        end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
                </el-form-item>
                <el-form-item>
                    <!-- <el-select placeholder="选择品牌" v-model="formInline.brand_id" @change="brandChange">
                        <el-option v-for="item in brandList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select> -->
                    <el-select filterable placeholder="选择门店" v-model="formInline.store_id" @change="storeChange">
                        <el-option v-for="item in storeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                    <el-select placeholder="选择区域" v-model="formInline.area_id" style="width:100px">
                        <el-option v-for="item in areaList" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <dj-button size="small" @search="searchClick" />
                </el-form-item>
            </el-form>
            <el-table  highlight-current-row show-summary :data="list" border :summary-method="getSummaries" size="small">
                <el-table-column align="center" prop="datetime" label="时间"></el-table-column>
                <el-table-column align="center" prop="area_name" label="机台区域"></el-table-column>
                <el-table-column align="center" prop="no" label="机台编号"></el-table-column>
                <el-table-column align="center" prop="name" label="机台名称"></el-table-column>
                <el-table-column align="center" prop="money_aliy" label="支付宝"></el-table-column>
                <el-table-column align="center" prop="money_wx" label="微信"></el-table-column>
                <el-table-column align="center" prop="cash_actual" label="现金"></el-table-column>
                <el-table-column align="center" prop="nick" label="报账店员"></el-table-column>
            </el-table>
        </div>
    </div>
</template>

<script>
    import {
        getList,
        getBrandList,
        getStoreList,
        getAreaList,
    } from "@/api/member/coin_machine_detail";
    import { pickerOptionsF, pickerDateF, optionMaxTime } from '@/utils';
    export default {
        data() {
            return {
                listLoading: false,
                storeList: [],
                areaList: [],
                formInline: {
                    area_id: '',
                    store_id: '',
                    begin_time: '',
                    end_time: '',
                },
                list: [],
                pickerOptions: pickerOptionsF(-1),
                pickerDate: pickerDateF(-1,-1)
            };
        },
        created() {
            this.getStore();
        },
        methods: {
            getSummaries(param) {
                const { columns, data } = param;
                const sums = [];
                columns.forEach((column, index) => {
                    if (index === 0) {
                        sums[index] = '合计';
                        return;
                    }
                    if (index === 1 || index === 2 || index === 3) {
                        sums[index] = '-';
                        return;
                    }
                    const values = data.map(item => Number(item[column.property]));
                    if (index > 0) {
                        if (!values.every(value => isNaN(value))) {
                            sums[index] = values.reduce((prev, curr) => {
                                const value = Number(curr);
                                if (!isNaN(value)) {
                                    return prev + curr;
                                } else {
                                    return prev;
                                }
                            }, 0);
                            sums[index] = (sums[index] * 1).toFixed(2) * 1;
                        } else {
                            sums[index] = '-';
                        }
                    }
                });
                return sums;
            },
            // getBrand() {
            //     getBrandList({ org: 1, category_id: 311 }).then(response => {
            //         this.brandList = response;
            //         this.formInline.brand_id = response[0].id
            //         this.getStore()
            //     });
            // },
            // brandChange() {
            //     // this.formInline.store_id = '';
            //     this.formInline.area_id = '';
            //     this.getStore();
            // },
            getStore() {
                getStoreList({  category_id: 311 }).then(response => {
                    this.storeList = response;
                    let data = response.filter(res => 
                        res.id == this.$store.getters.optionId
                    )
                    if(data.length>0){
                        this.formInline.store_id = data[0].id;
                    }
                    this.storeChange();
                });
            },
            storeChange() {
                getAreaList({ sid: this.formInline.store_id }).then(response => {
                    response.unshift({ id: '', name: '全部区域' });
                    this.areaList = response;
                    this.formInline.area_id = response[0].id;
                    if (this.list == null) {
                        this.searchClick();
                    }
                });
            },
            // 搜索
            searchClick() {
                optionMaxTime(this.pickerDate).then(() => {//最大可选择时间区间
                    this.formInline.begin_time = this.pickerDate ? this.pickerDate[0] : '';
                    this.formInline.end_time = this.pickerDate ? this.pickerDate[1] : '';
                    this.fetchData();
                });
            },
            // 列表获取
            fetchData() {
                this.listLoading = true;
                getList(this.formInline).then(response => {
                    this.list = response;
                    this.listLoading = false;
                }).catch(res=>{
                    this.listLoading=false;
                })
            },
        }
    };
</script>